package com.trkj.service.wxy;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wxy.ExamArrangeDao;
import com.trkj.dao.wxy.ExamDao;
import com.trkj.dao.wxy.OverviewDao;
import com.trkj.pojo.ExamArrange;
import com.trkj.pojo.ExamPaper;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.vo.wxy.ExamArrangePageVo;


@Service
@Transactional(rollbackFor = Exception.class)
public class ExamArrangeService {
	@Autowired
	private ExamDao examDao;
	@Autowired
	private ExamArrangeDao examArrangeDao;
	
	@Autowired
	private OverviewDao overviewDao;
	/**
	 * 查询考试安排所有的记录分页
	 */
	public List<ExamArrangePageVo> queryByExamArrangeQuestion(int page,int rows){
		List<ExamArrange> list = examArrangeDao.queryByExamArrange(new PageRequest(page-1, rows));
		List<ExamArrangePageVo> listExamArrangePageVo = new ArrayList<>();
		if(list != null){
			for(ExamArrange objs: list){
				ExamArrangePageVo examArrangePageVo = new ExamArrangePageVo();
				if(!objs.getExamPapers().isEmpty()){
					ExamPaper e =  objs.getExamPapers().get(0);
					examArrangePageVo.setEpTotal(e.getEpTotal());
					examArrangePageVo.setEpStarttime(e.getEpStarttime());
					examArrangePageVo.setEpEndtime(e.getEpEndtime());
					examArrangePageVo.setEpShichang(e.getEpShichang());
					examArrangePageVo.setClassSjName(e.getClassSjName());
				}
				ExamArrange r =  objs;
	
			
				examArrangePageVo.setArrangeId(r.getArrangeId());
				examArrangePageVo.setArrangeName(r.getArrangeName());
				examArrangePageVo.setArrangeUnifyEspecial(r.getArrangeUnifyEspecial());
				examArrangePageVo.setArrangeLookAnswer(r.getArrangeLookAnswer());
				examArrangePageVo.setArrangeNumberExaminee(r.getArrangeNumberExaminee());
				examArrangePageVo.setArrangeNumberAdministrator(r.getArrangeNumberAdministrator());
				examArrangePageVo.setArrangeAddress(r.getArrangeAddress());
				examArrangePageVo.setArrangeRemark(r.getArrangeRemark());
				examArrangePageVo.setArrangeStart(r.getArrangeStart());
				listExamArrangePageVo.add(examArrangePageVo);
			}
		}
		return listExamArrangePageVo;
		
	}
	
	/**
	 * 获取总记录数
	 * @return
	 */
	public long empByPageCount(){
		return examArrangeDao.count();
		
	}
	
	/**
	 * 新增
	 * @param exPaper
	 * @param exArrange
	 * @throws ServiceException
	 */
	public void addExamArrange(ExamPaper exPaper, ExamArrange exArrange) throws ServiceException{
		try {
			List<ExamPaper> list=new ArrayList<>();
			list.add(exPaper);
			exArrange.setExamPapers(list);
			
			examArrangeDao.save(exArrange);
		
			ExamPaper ex=examDao.findOne(exPaper.getEpId());
			List<ExamArrange> listExamArr=null;
			if(!ex.getExamArranges().isEmpty()){
				listExamArr =ex.getExamArranges();		
			}else{
				listExamArr=new ArrayList<>();
			}
			
			listExamArr.add(exArrange);
			ex.setExamArranges(listExamArr);
			examDao.save(ex);
		
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param arrangeId
	 * @throws ServiceException
	 */
	public void ksap_delete(Long arrangeId) throws ServiceException{
		try {
			examArrangeDao.delete(arrangeId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
	/**
	 * 考试安排编辑
	 * @param epId
	 * @param examPageVo
	 * @return
	 * @throws ServiceException
	 */
	public ExamArrangePageVo updateExamArrangeQuestion(Long arrangeId,String classSjName,ExamArrangePageVo examArrangePageVo) throws ServiceException{
		
		try {
			
			ExamArrange examArrange = examArrangeDao.findOne(arrangeId);
			ExamPaper examPaper = examDao.queryByExamPaperClassSjName(classSjName);
			
			examArrangePageVo.setEpId(examPaper.getEpId());
			examArrangePageVo.setArrangeId(arrangeId);
			examArrangePageVo.setClassSjName(classSjName);
			examArrangePageVo.setArrangeName(examArrange.getArrangeName());
			examArrangePageVo.setEpTotal(examPaper.getEpTotal());
			examArrangePageVo.setArrangQualified(examArrange.getArrangQualified());
			examArrangePageVo.setEpShichang(examPaper.getEpShichang());
			examArrangePageVo.setEpGrade(examPaper.getEpGrade());
			examArrangePageVo.setEpWay(examPaper.getEpWay());
			examArrangePageVo.setArrangeRemark(examArrange.getArrangeRemark());
			examArrangePageVo.setEpStarttime(examPaper.getEpStarttime());
			examArrangePageVo.setEpEndtime(examPaper.getEpEndtime());
			examArrangePageVo.setArrangeAddress(examArrange.getArrangeAddress());
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
		return examArrangePageVo;
		
	}
	
	/**
	 * 考试安排编辑之后保存
	 * @param epId
	 * @param examPageVo
	 * @throws ServiceException
	 */
	public void updateSavaExamArrangeQuestion(Long arrangeId,String classSjName,ExamArrangePageVo examArrangePageVo) throws ServiceException{
		try {
			ExamArrange examArrange = examArrangeDao.findOne(arrangeId);
			ExamPaper examPaper = examDao.queryByExamPaperClassSjName(classSjName);
			
			examArrange.setArrangeName(examArrangePageVo.getArrangeName());
			examArrange.setArrangQualified(examArrangePageVo.getArrangQualified());
			examArrange.setArrangeRemark(examArrangePageVo.getArrangeRemark());
			examArrange.setArrangeAddress(examArrangePageVo.getArrangeAddress());
			examArrange.setArrangeLookAnswer(examArrangePageVo.getArrangeLookAnswer());
			examArrange.setArrangeNumberExaminee(examArrangePageVo.getArrangeNumberExaminee());
			examArrange.setArrangeNumberAdministrator(examArrangePageVo.getArrangeNumberAdministrator());
			examArrange.setArrangeStart(examArrangePageVo.getArrangeStart());
			examArrange.setArrangeUnifyEspecial(examArrangePageVo.getArrangeUnifyEspecial());
			
			List<ExamArrange> listExamArr=null;
			if(!examPaper.getExamArranges().isEmpty()){
				listExamArr =examPaper.getExamArranges();		
			}else{
				listExamArr=new ArrayList<>();
				listExamArr.add(examArrange);
			}
			
			examPaper.setExamArranges(null);
			examPaper.setExamArranges(listExamArr);
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
	/**
	 * 查询考试安排所有的记录分页
	 */
	public List<ExamArrangePageVo> queryByStaffCjGanl(int page,int rows){
		List<Object[]> list = examArrangeDao.queryByStaffCjGanl(new PageRequest(page-1, rows));
		List<ExamArrangePageVo> listExamArrangePageVo = new ArrayList<>();
		if(list != null){
			for(Object[] objs: list){
				ExamArrangePageVo examArrangePageVo = new ExamArrangePageVo();
				ExamPaper e =  (ExamPaper) objs[2];
				
				examArrangePageVo.setEpDefen(e.getEpDefen());
				examArrangePageVo.setEpStarttime(e.getEpStarttime());
				examArrangePageVo.setEpEndtime(e.getEpEndtime());
				examArrangePageVo.setEpShichang(e.getEpShichang());
				examArrangePageVo.setClassSjName(e.getClassSjName());
				
				ExamArrange a =  (ExamArrange) objs[0];
				examArrangePageVo.setArrangeName(a.getArrangeName());
				
				StaffInformation s = (StaffInformation) objs[1];
				examArrangePageVo.setStainId(s.getStainId());
				examArrangePageVo.setStainName(s.getStainName());
				
				listExamArrangePageVo.add(examArrangePageVo);
			}
		}
		return listExamArrangePageVo;
	}
}
